package com.leyantech.ecp.agenx.task.di;

import com.leyantech.ai.DialogueReply;
import com.leyantech.chaos.kafka.KafkaConsumerClient;
import com.leyantech.ecp.agenx.common.converter.output.DialogueReplyConverter;
import com.leyantech.ecp.agenx.common.converter.output.PinDuoDuoReplyConverter;
import com.leyantech.ecp.agenx.common.di.HelaModule;
import com.leyantech.ecp.agenx.common.di.MessageDispatcherModule;
import com.leyantech.ecp.agenx.common.di.RedissonModule;
import com.leyantech.ecp.agenx.common.serialization.DialogueReplyDeserializer;
import com.leyantech.ecp.agenx.session.client.SessionClientModule;
import com.leyantech.ecp.agenx.task.consumer.TakeActionMessageConsumer;
import com.leyantech.ecp.agenx.task.consumer.TaskMessageConsumer;
import com.leyantech.ecp.agenx.task.converter.output.PinDuoDuoTaskMessageConverter;
import com.leyantech.ecp.agenx.task.converter.output.TaskMessageConverter;

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
import io.vertx.core.json.JsonObject;
import io.vertx.kafka.client.serialization.JsonObjectDeserializer;
import org.apache.kafka.common.serialization.StringDeserializer;

/**
 * @author eahau, {@literal <eahau@foxmail.com>}
 * @date 2022-11-16.
 */
public class TaskModule extends AbstractModule {

  @Override
  protected void configure() {

    install(new HelaModule());

    install(new SessionClientModule());

    install(new RedissonModule());

    install(new MessageDispatcherModule());

    bind(DialogueReplyConverter.class).to(PinDuoDuoReplyConverter.class).asEagerSingleton();

    bind(new TypeLiteral<TaskMessageConverter<JsonObject>>() {
    }).to(PinDuoDuoTaskMessageConverter.class)
        .asEagerSingleton();

  }

  @Singleton
  @Provides
  KafkaConsumerClient<String, JsonObject> provideTaskMessageConsumerClient(
      TaskMessageConsumer processor) {
    return KafkaConsumerClient.<String, JsonObject>newBuilder("task")
        .setProcessor(processor)
        .setKeyDeserializer(StringDeserializer.class)
        .setValueDeserializer(JsonObjectDeserializer.class)
        .build();
  }

  @Singleton
  @Provides
  KafkaConsumerClient<String, DialogueReply> provideTakeActionConsumerClient(
      TakeActionMessageConsumer processor) {
    return KafkaConsumerClient.<String, DialogueReply>newBuilder("take-action")
        .setProcessor(processor)
        .setKeyDeserializer(StringDeserializer.class)
        .setValueDeserializer(DialogueReplyDeserializer.class)
        .build();
  }

}
